package com.controller;

import com.entry.User;
import com.google.gson.Gson;

import com.service.UserService;
import com.service.impl.UserServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/systerm/*")
public class DirectoryServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //0.处理乱码：
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        //1.获得请求路径
        String  url=request.getRequestURI();
        String methodName=url.substring(url.lastIndexOf("/")+1);
        Class cla= DirectoryServlet.class;
        try {
            Method method = cla.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(cla.newInstance(),request,response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
//
//    //查询全部数据
//    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
//        ArrayList<User> books=bookService.selectAll();
//        Gson gson=new Gson();
//        response.getWriter().println(gson.toJson(books));
//
////        ArrayList<Book> books = bookService.selectAll();
////        Gson gson=new Gson();
////        response.getWriter().println(gson.toJson(books));
//
//    }
//
//    //按条件查询数据
//    public void queryByBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
//        //接收第一个参数：
//        String query = request.getParameter("query");
//        String str = request.getParameter("str");
//        //准备参数：
//        User book=new User();
//        if("1".equals(query)){
//            book.setBookName(str);
//        }else if("2".equals(query)){
//            book.setBookAuthor(str);
//        }else if("3".equals(query)){
//            book.setBookPublish(str);
//        }
//        ArrayList<User> books = bookService.selectQuery(book);
//        Gson gson=new Gson();
//        response.getWriter().println(gson.toJson(books));
//    }
//
//    public void addBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
//        //获得参数：
//        String bname=request.getParameter("bname");
//        String aname=request.getParameter("aname");
//        String cname=request.getParameter("cname");
//        String page=request.getParameter("page");
//        String price=request.getParameter("price");
//        //准备参数：
//        User book=new User();
//        book.setBookName(bname);
//        book.setBookAuthor(aname);
//        book.setBookPublish(cname);
//        book.setBookPage(Integer.parseInt(page));
//        book.setBookPrice(Double.parseDouble(price));
//
//        boolean b = bookService.insertBook(book);
//        response.getWriter().println(b);
//    }
//
//    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
//
//    }
//
//    public void queryByid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
//         int bid=Integer.parseInt(request.getParameter("bid"));
//         User book=bookService.selectById(bid);
//         Gson gson=new Gson();
//         response.getWriter().println(gson.toJson(book));
//
//    }
//
//    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String bname=request.getParameter("bname");
//        String aname=request.getParameter("aname");
//        String cname=request.getParameter("cname");
//        int page=Integer.parseInt(request.getParameter("page"));
//        double price=Double.parseDouble(request.getParameter("price"));
//        String date=request.getParameter("date");
//        int bid=Integer.parseInt(request.getParameter("bid"));
//
//        User book=new User(bid,bname,aname,cname,page,price,date);
//
//        boolean b = bookService.updateBook(book);
//        response.getWriter().println(b);
//    }
}